package com.cityfood.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfSessionsMapper;
import com.cityfood.pojo.CfSessions;
import com.cityfood.pojo.CfSessionsExample;
import com.cityfood.pojo.CfSessionsExample.Criteria;
import com.cityfood.service.SessionService;

import java.util.List;

@Service
public class SessionServiceImpl implements SessionService {
	@Autowired
	private CfSessionsMapper sessionsMapper;
	
	@Override
	public int createSessionMap(CfSessions map){
		return sessionsMapper.insert(map);
	}
	/**
	 * 小程序定时发送getSession请求，同步更新sessionId，保持服务器session存活；
	 * sessionKey前端判定失效，重新登陆的时候sessionId和sessionKey同步更新
	 */
	
	@Override
	public int refreshSessionMap(CfSessions map){
		return sessionsMapper.updateByPrimaryKeySelective(map);
	}
	/**
	 * sessionKey没有失效情况下，通过sessionId查询sessionKey
	 */
	@Override
	public String getSessionKey(String sessionId){
		CfSessionsExample example = new CfSessionsExample();
		Criteria criteria = example.createCriteria();
		criteria.andSessionidEqualTo(sessionId);
		List<CfSessions> list = sessionsMapper.selectByExample(example);
		return list.get(0).getSessionkey();
	}
	
	@Override
	public CfSessions getSessionMap(int uid){
		CfSessionsExample example = new CfSessionsExample();
		Criteria criteria = example.createCriteria();
		criteria.andUidEqualTo(uid);
		List<CfSessions> list = sessionsMapper.selectByExample(example);
		return  list.get(0);
	}

}
